const { Service } = require('egg');

class HomeService extends Service {
  async getShopTotal() {
    try {
      // 统计相关
      let allList = await this.app.model.Order.findAll();
      let orderCount = allList.length;
      let confirmList = await this.app.model.Order.findAll(
        { where: { order_isConfirm: 1 } }
      );
      let SalesVal = 0;
      let SalesCount = confirmList.length;
      confirmList.forEach(item=>{
        SalesVal += item.order_price;
      });
      let wxuserList = await this.app.model.Wxuser.findAll();
      let wxuserCount = wxuserList.length;
      // 订单相关
      let waitPayList = await this.app.model.Order.findAll(
        { where: { order_isPay: 0 } }
      );
      let waitSendList = await this.app.model.Order.findAll({
        where: {
          order_isPay: 1,
          order_isSend: 0
        }
      });
      let waitConfirmList = await this.app.model.Order.findAll({
        where: {
          order_isConfirm: 0,
          order_isSend: 1
        }
      });
      let waitPayCount = waitPayList.length;
      let waitSendCount = waitSendList.length;
      let waitConfirmCount = waitConfirmList.length;
      return {
        orderCount,
        SalesVal,
        SalesCount,
        wxuserCount,
        waitPayCount,
        waitSendCount,
        waitConfirmCount
      };
    } catch (err) {
      return false;
    }

  }

}

module.exports = HomeService;
